package com.rzj.sys_lab_assistant.controller;

import com.rzj.sys_lab_assistant.entity.Admin;
import com.rzj.sys_lab_assistant.entity.Assistant;
import com.rzj.sys_lab_assistant.entity.Student;
import com.rzj.sys_lab_assistant.entity.Technician;
import com.rzj.sys_lab_assistant.entity.bo.AssistantBO;
import com.rzj.sys_lab_assistant.service.IAdminService;
import com.rzj.sys_lab_assistant.service.IAssistantService;
import com.rzj.sys_lab_assistant.service.IStudentService;
import com.rzj.sys_lab_assistant.service.ITechnicianService;
import com.rzj.sys_lab_assistant.utils.ResultsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @program: sys_lab_assistant
 * @description:
 * @author: 作者
 * @create: 2021-07-03 19:03
 */
@Controller
public class LoginController {

    @Autowired
    IAssistantService assistantService;
    @Autowired
    IStudentService studentService;
    @Autowired
    IAdminService adminService;
    @Autowired
    ITechnicianService technicianService;


    ResultsUtils resultsUtils;

    @RequestMapping("toRegister")
    public String toRegister() {
        return "register";
    }

    @RequestMapping("/")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("toIndex")
    public String toIndex() {
        return "index";
    }

    @RequestMapping("loginTest")
    @ResponseBody
    @CrossOrigin("*")
    public Map<String, String> loginTest(@RequestBody Admin admin){
        System.out.println("admin==>"+admin.toString());
        return resultsUtils.resultsMap("success", "测试成功！");
    }



    @RequestMapping("login/check")
    @ResponseBody
    public Map<String, String> check(@RequestParam("username") String username,
                                     @RequestParam("password") String password,
                                     @RequestParam("type") String type,
                                     HttpSession session) {
        //System.out.println("type"+type);
        if (type.equals("0")) {
            Admin admin = adminService.getAdminById(username);
            if (admin != null) {
                if (admin.getPassword().equals(password)) {
                    session.setAttribute("type", type);
                    session.setAttribute("admin", admin);
                    return resultsUtils.resultsMap("success", "登录成功！");
                } else {
                    return resultsUtils.resultsMap("error", "密码错误！");
                }
            } else {
                return resultsUtils.resultsMap("error", "账户不存在！");
            }

        }
        if (type.equals("1")) {
            Technician technician = technicianService.getTechnicianById(username);
            if (technician != null) {
                if (technician.getPassword().equals(password)) {
                    session.setAttribute("type", type);
                    session.setAttribute("technician", technician);
                    return resultsUtils.resultsMap("success", "登录成功！");
                } else {
                    return resultsUtils.resultsMap("error", "密码错误！");
                }
            } else {
                return resultsUtils.resultsMap("error", "账户不存在！");
            }
        }
        if (type.equals("2")) {
            Student student = studentService.getStudentById(username);
            if (student != null) {
                if (student.getStudentPwd().equals(password)) {
                    session.setAttribute("type", type);
                    session.setAttribute("student", student);
                    return resultsUtils.resultsMap("success", "登录成功！");
                } else {
                    return resultsUtils.resultsMap("error", "密码错误！");
                }
            } else {
                return resultsUtils.resultsMap("error", "账户不存在！");
            }
        }
        if (type.equals("3")) {
            AssistantBO assistant = assistantService.getAssistantById(Integer.valueOf(username));
            if (assistant != null) {
                if (assistant.getAPwd().equals(password)) {
                    session.setAttribute("type", type);
                    session.setAttribute("assistant", assistant);
                    return resultsUtils.resultsMap("success", "登录成功！");
                } else {
                    return resultsUtils.resultsMap("error", "密码错误！");
                }
            } else {
                return resultsUtils.resultsMap("error", "账户不存在！");
            }
        }
        if (type.equals("4")) {
            Admin admin = adminService.getAdminById(username);
            if (admin != null) {
                if (admin.getPassword().equals(password)) {
                    session.setAttribute("type", type);
                    session.setAttribute("work", admin);
                    return resultsUtils.resultsMap("success", "登录成功！");
                } else {
                    return resultsUtils.resultsMap("error", "密码错误！");
                }
            } else {
                return resultsUtils.resultsMap("error", "账户不存在！");
            }
        }

        return resultsUtils.resultsMap("error", "未选择角色！");

    }

    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.removeAttribute("type");
        return "redirect:http://localhost:9527/sysLab/";
    }


}
